Exercises
Bastions and Basilisks Bug
You're building an in-browser role-playing game, Bastions and Basilisks, but you've hit a snag! The wrong values are being shown to the user when their character levels up.
Your mission is to fix the bug.
Acceptance Criteria:
- When the “Level up” button is clicked, the
alert
that pops up should show the updated values for strength/dexterity/intelligence. - For example, when clicking the button for the first time, “strength” should be displayed as 7, not 6.
Code Playground
import React from 'react';
function Character() {
const [strength, setStrength] = React.useState(6);
const [dexterity, setDexterity] = React.useState(9);
const [intelligence, setIntelligence] = React.useState(15);
function triggerLevelUp() {
setStrength(strength + 1);
setDexterity(dexterity + 2);
setIntelligence(intelligence + 3);
window.alert(`
Congratulations! Your hero now has the following stats:
Str: ${strength}
Dex: ${dexterity}
Int: ${intelligence}
`);
}
return (
<div className="wrapper">
<img
alt="8-bit wizard character"
src="https://sandpack-bundler.vercel.app/img/mage-sprite.gif"
/>
<button onClick={triggerLevelUp}>
Level Up
</button>
</div>
);
}
export default Character;
Solution:
Counter 2.0
The counter has been upgraded, and now supports several functions.
The buttons have already been added to the page, but they don't do anything yet. Your job is to wire up the buttons.
Acceptance Criteria:
- The button should increase the count by 1.
- The button should increase the count by 10.
- The button should reset the count to 0.
- The button should set the count to a random number between 1 and 100.
- The button should decrease the count by 10.
- The button should decrease the count by 1.
Code Playground
import React from 'react';
import { ChevronUp, ChevronsUp, ChevronDown, ChevronsDown, RotateCcw, Hash } from 'react-feather'
function Counter() {
const [count, setCount] = React.useState(0);
return (
<div className="wrapper">
<p>
<span>Current value:</span>
<span className="count">
{count}
</span>
</p>
<div className="button-row">
<button>
<ChevronUp />
<span className="visually-hidden">
Increase slightly
</span>
</button>
<button>
<ChevronsUp />
<span className="visually-hidden">
Increase a lot
</span>
</button>
<button>
<RotateCcw />
<span className="visually-hidden">
Reset
</span>
</button>
<button>
<Hash />
<span className="visually-hidden">
Set to random value
</span>
</button>
<button>
<ChevronsDown />
<span className="visually-hidden">
Decrease a lot
</span>
</button>
<button>
<ChevronDown />
<span className="visually-hidden">
Decrease slightly
</span>
</button>
</div>
</div>
);
}
export default Counter;
Solution: